/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: UserLoginControllerImpl
 * Author:   Allen
 * Date:     2020/8/27 20:21
 * Description: 用户登录实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.bim.marvel.auth.client.resource.client.OauthServerClient;
import com.bim.marvel.auth.client.resource.config.Oauth2Properties;
import com.bim.marvel.auth.client.resource.model.dto.TokenDTO;
import com.bim.marvel.openapi.client.constant.OpenApiCodeEnum;
import com.bim.marvel.openapi.client.query.LoginQuery;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.constant.GrantTypeConstants;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.openapi.core.service.impl.DealerUserService;
import com.bim.marvel.rest.DealerUserLoginController;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈用户登录接口功能实现类〉
 *
 * @author Allen
 * @date 2020/8/27
 * @since 1.0.0
 */
@Slf4j
@RestController
public class DealerUserLoginControllerImpl implements DealerUserLoginController {

    /**
     * oauth客户端接口
     */
    @Autowired
    private OauthServerClient oauthClient;

    /**
     * oauth2配置
     */
    @Autowired
    private Oauth2Properties oauth2Properties;

    /**
     * 操作用户的服务类
     */
    @Autowired
    DealerUserService userService;

    @Override
    public SimpleVO<TokenDTO> userLogin(LoginQuery loginQuery) {
        SimpleVO simpleVO = null;
        try {
            //判断员工是否禁用或允许登录
            TokenDTO token = oauthClient.token(oauth2Properties.basicAuth(), loginQuery.getLoginName(),
                    loginQuery.getPassword(), GrantTypeConstants.PASSWORD);
            simpleVO = new SimpleVO(token);
        } catch (UsernameNotFoundException userNotFoundException) {
            simpleVO.setMessage(userNotFoundException.getMessage());
        } catch (FeignException error) {
            simpleVO.setMessage(OpenApiCodeEnum.USER_PSW_ERROR.message());
        }
        return simpleVO;
    }

    @Override
    public SimpleVO<TokenDTO> refreshToken(String refreshToken) {
        SimpleVO simpleVO;
        try {
            //判断员工是否禁用或允许登录
            TokenDTO token = oauthClient.refreshToken(oauth2Properties.basicAuth(), GrantTypeConstants.REFRESH_TOKEN,
                    refreshToken);
            simpleVO = new SimpleVO(token);
        } catch (FeignException error) {
            String codeEnum;
            simpleVO = new SimpleVO<>(CodeEnum.UNKNOWN_ERROR);
            switch (error.status()) {
                default:
                    codeEnum = OpenApiCodeEnum.REFRESH_TOKEN_ERROR.message();
                    break;
            }
            simpleVO.setMessage(codeEnum);
        }
        return simpleVO;
    }
}
